////////////////////////////////////////////////////////////////////////////////
//      
//      Kingsoft File for shp routines file
//      
//      File      : shp_skin.h
//      Comment   : simple html parser skin class
//					
//      Create at : 2012-12-28 20:27:50
//      Create by : chenguicheng
//      
////////////////////////////////////////////////////////////////////////////////

#pragma once
#include "shp_def.h"
#include "common/shp_draw.h"

#define SHP_SKIN_ITEM_KEY					TEXT("skin")

class shp_img_mgr
{
public:
	shp_img_mgr()
	{

	}

	~shp_img_mgr()
	{
		
	}

	void un_init()
	{
		POSITION pos = m_map_img.GetStartPosition();
		while ( pos != NULL ) 
		{
			shpimg img = m_map_img.GetNextValue( pos );
			shp_draw::destroy_img( img );
		}

		m_map_img.RemoveAll();
	}

	static shp_img_mgr& Instance()
	{
		static shp_img_mgr _obj;
		return _obj;
	}

	inline shpimg get_skin( shpstr& str_img )
	{
		str_img.MakeLower();
		CAtlMap<shpstr, shpimg>::CPair *p_find = m_map_img.Lookup( str_img );
		if( p_find )
			return p_find->m_value;
		else
		{
			shpimg img = shp_draw::load_img( str_img );
			m_map_img.SetAt( str_img, img );
			return img;
		}
	}

protected:

	CAtlMap<shpstr, shpimg>		m_map_img;

};


class shp_skin : public IShpParse
{
public:

	shp_skin()
	:m_x_offset(0)
	,m_y_offset(0)
	,m_i_left(0)
	,m_i_top(0)
	,m_i_subwidth(0)
	,m_i_alpha(SHP_NO_ALPHA)
	,m_clr_center(CLR_INVALID)
	{

	};

	~shp_skin()
	{

	};

	//set_attribute( p, m )
	BEGIN_KEY_MAP()

		ADD_INT_KEY( L"left",			m_i_left )
		ADD_INT_KEY( L"top",			m_i_top )
		ADD_INT_KEY( L"subwidth",		m_i_subwidth )
		ADD_INT_KEY( L"y_offset",		m_y_offset )
		ADD_INT_KEY( L"x_offset",		m_x_offset )
		ADD_INT_KEY( L"alpha",			m_i_alpha )

		ADD_CLR_KEY( L"color",			m_clr_center )
		ADD_STR_KEY( L"name",			m_str_name )
		ADD_STR_KEY( L"src",			m_str_src )

	END_KEY_MAP()

	CSize& get_skin_size()
	{
		shpimg img = shp_img_mgr::Instance().get_skin( m_str_src );
		return shp_draw::get_img_size( img );
	}

	void draw( shpdc& dc, int i_state, CRect rect, CRect& draw_rect )
	{
		shpimg img = shp_img_mgr::Instance().get_skin( m_str_src );
		CSize size = shp_draw::get_img_size( img );
		
		CRect rc_bmp( m_x_offset, m_y_offset, size.cx, size.cy );
		if( m_i_subwidth != 0 )
		{
			rc_bmp.left += m_i_subwidth * i_state;
			rc_bmp.right = rc_bmp.left + m_i_subwidth;
		}

		if( m_i_left == 0 && m_i_top == 0 )
		{
			if( RectInRect( draw_rect, rect ) )
			{
				rc_bmp.left += rect.left - draw_rect.left;
				rc_bmp.top += rect.top - draw_rect.top;
				rc_bmp.right = rc_bmp.left + draw_rect.Width();
				rc_bmp.bottom = rc_bmp.top + draw_rect.Height();
				shp_draw::draw_bmp( dc, img, rc_bmp, draw_rect.left, draw_rect.top, m_i_alpha );
			}
			else
				shp_draw::draw_bmp( dc, img, rc_bmp, rect, m_i_alpha );

			return;
		}
  
		int i_width = ( m_i_left != 0 ? m_i_left : rc_bmp.Width() );
		int i_height = ( m_i_top != 0 ? m_i_top : rc_bmp.Height() );

		//left_top
		CRect rc_src( rc_bmp );
		rc_src.right = rc_src.left + i_width;
		rc_src.bottom = rc_src.top + i_height;
		shp_draw::draw_bmp( dc, img, rc_src, rect.left, rect.top, m_i_alpha );

		if( m_i_left != 0 )
		{
			//center_top
			rc_src.left = rc_src.right;
			rc_src.right = rc_bmp.right - i_width;
			rc_src.bottom = rc_src.top + i_height;
			CRect rc_draw( rect );
			rc_draw.bottom = rc_draw.top + i_height;
			rc_draw.left = rect.left + i_width;
			rc_draw.right = rect.right - i_width;
			shp_draw::draw_bmp( dc, img, rc_src, rc_draw, m_i_alpha );

			//right_top
			rc_src = rc_bmp;
			rc_src.left = rc_src.right - i_width;
			rc_src.bottom = rc_src.top + i_height;
			shp_draw::draw_bmp( dc, img, rc_src, rect.right - i_width, rect.top, m_i_alpha );
		}

		if( m_i_top != 0 )
		{
			//left_center
			rc_src = rc_bmp;
			rc_src.right = rc_src.left + i_width;
			rc_src.top = rc_src.top + i_height;
			rc_src.bottom = rc_src.bottom - i_height;

			CRect rc_draw( rect );
			rc_draw.right = rc_draw.left + i_width;
			rc_draw.top = rc_draw.top + i_height;
			rc_draw.bottom = rc_draw.bottom - i_height;
			shp_draw::draw_bmp( dc, img, rc_src, rc_draw, m_i_alpha );

			//left_bottom
			rc_src = rc_bmp;
			rc_src.right = rc_src.left + i_width;
			rc_src.top = rc_src.bottom - i_height;
			shp_draw::draw_bmp( dc, img, rc_src, rect.left, rect.bottom - i_height, m_i_alpha );
		}

		if( m_i_left != 0 && m_i_top != 0 )
		{
			//bottom_center
			rc_src.left = rc_src.right;
			rc_src.right = rc_bmp.right - i_width;
			rc_src.top = rc_src.bottom - i_height;
			CRect rc_draw( rect );
			rc_draw.top = rc_draw.bottom - i_height;
			rc_draw.left = rect.left + i_width;
			rc_draw.right = rect.right - i_width;
			shp_draw::draw_bmp( dc, img, rc_src, rc_draw, m_i_alpha );

			//right_center
			rc_src = rc_bmp;
			rc_src.left = rc_src.right - i_width;
			rc_src.top = rc_src.top + i_height;
			rc_src.bottom = rc_src.bottom - i_height;

			rc_draw = rect;
			rc_draw.left = rc_draw.right - i_width;
			rc_draw.top = rc_draw.top + i_height;
			rc_draw.bottom = rc_draw.bottom - i_height;
			shp_draw::draw_bmp( dc, img, rc_src, rc_draw, m_i_alpha );
			

			//bottom_right
			rc_src= rc_bmp;
			rc_src.left = rc_src.right - i_width;
			rc_src.top = rc_src.bottom - i_height;
			shp_draw::draw_bmp( dc, img, rc_src, rect.right - i_width, rect.bottom - i_height, m_i_alpha );

			if( m_clr_center != CLR_INVALID )
			{
				rect.DeflateRect( i_width, i_height, i_width, i_height );
				shp_draw::fill_rect( dc, rect, m_clr_center, m_i_alpha );
			}
		}
	}

public:

	int			m_x_offset;
	int			m_y_offset;
	int			m_i_left;
	int			m_i_top;
	int			m_i_subwidth;
	int			m_i_alpha;

	shpstr		m_str_name;
	shpstr		m_str_src;
	shpclr		m_clr_center;
	
};


